﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.WpfClient.Accessibility
{
    public partial  class AccessibilityCacheServerSyncProvider : Microsoft.Synchronization.Data.Server.DbServerSyncProvider
    {
        

        partial void OnInitialized();
       

        [Microsoft.Practices.Unity.InjectionConstructor]
        public AccessibilityCacheServerSyncProvider(string connectionString)
        {            
            this.InitializeConnection(connectionString);
            this.InitializeSyncAdapters();
            this.InitializeNewAnchorCommand();
            this.OnInitialized();


            
            this.ApplyChangeFailed += new EventHandler<Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs>(AccessibilityCacheServerSyncProvider_ApplyChangeFailed);
        }

        void AccessibilityCacheServerSyncProvider_ApplyChangeFailed(object sender, Microsoft.Synchronization.Data.ApplyChangeFailedEventArgs e)
        {
            switch (e.Conflict.ConflictType)
            {
                case Microsoft.Synchronization.Data.ConflictType.ClientDeleteServerUpdate:
                    e.Action = Microsoft.Synchronization.Data.ApplyAction.Continue;
                    break;
                case Microsoft.Synchronization.Data.ConflictType.ClientInsertServerInsert:
                    e.Action = Microsoft.Synchronization.Data.ApplyAction.Continue;
                    break;
                case Microsoft.Synchronization.Data.ConflictType.ClientUpdateServerDelete:
                    e.Action = Microsoft.Synchronization.Data.ApplyAction.RetryWithForceWrite;
                    break;
                case Microsoft.Synchronization.Data.ConflictType.ClientUpdateServerUpdate:
                    e.Action = Microsoft.Synchronization.Data.ApplyAction.Continue;
                    break;
                case Microsoft.Synchronization.Data.ConflictType.ErrorsOccurred:
                    break;
                case Microsoft.Synchronization.Data.ConflictType.Unknown:
                    break;
                default:
                    break;
            }
            

        }
        public AccessibilitySyncAdapter AccessibilityAdapter { get; set; }
        private void InitializeConnection(string connectionString)
        {
            this.Connection = new System.Data.SqlClient.SqlConnection(connectionString);
        }
        private void InitializeSyncAdapters()
        {

            this.AccessibilityAdapter = new AccessibilitySyncAdapter();
            this.SyncAdapters.Add(this.AccessibilityAdapter);
        }
        private void InitializeNewAnchorCommand()
        {         
            this.SelectNewAnchorCommand = new System.Data.SqlClient.SqlCommand();
            this.SelectNewAnchorCommand.CommandText = "Select @sync_new_received_anchor = CHANGE_TRACKING_CURRENT_VERSION()";
            this.SelectNewAnchorCommand.CommandType = System.Data.CommandType.Text;
            System.Data.SqlClient.SqlParameter selectnewanchorcommand_sync_new_received_anchorParameter = new System.Data.SqlClient.SqlParameter("@sync_new_received_anchor", System.Data.SqlDbType.BigInt);
            selectnewanchorcommand_sync_new_received_anchorParameter.Direction = System.Data.ParameterDirection.Output;
            this.SelectNewAnchorCommand.Parameters.Add(selectnewanchorcommand_sync_new_received_anchorParameter);                        
        }

    }
}
